package com.example.zengfansheng.fragmentstatedemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {

    private static final String TAG = "hacket";
    private TextView mTv;
    private String txt;

    public static MyFragment newInstance(String text) {

        Bundle args = new Bundle();

        MyFragment fragment = new MyFragment(text);
        fragment.setArguments(args);
        return fragment;
    }

    public MyFragment() {
    }

    /**
     * 通过构造方法传递进来的数据，在fragment销毁重建时，不会被保存
     *
     * @param txt
     */
    public MyFragment(String txt) {
        this.txt = txt;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("data", txt);
        Log.d(TAG, getClass().getSimpleName() + " , " + "onSaveInstanceState , save data!");
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
        super.onInflate(context, attrs, savedInstanceState);

        Log.d(TAG, getClass().getSimpleName() + " , " + "onInflate() , savedInstanceState:" + savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Log.d(TAG, getClass().getSimpleName() + " , " + "1、onAttach()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, getClass().getSimpleName() + " , " + "2、onCreate() , savedInstanceState:" + savedInstanceState);
        if (savedInstanceState != null) {
            txt = savedInstanceState.getString("data");
            Log.d(TAG, getClass().getSimpleName() + " , " + "onCreate() , savedInstanceState:" + savedInstanceState);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, getClass().getSimpleName() + " , " + "3、onCreateView() , savedInstanceState:" + savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        mTv = (TextView) view.findViewById(R.id.frag_tv);
        mTv.setText(txt);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, getClass().getSimpleName() + " , " + "4、onActivityCreated() , savedInstanceState:"
                + savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, getClass().getSimpleName() + " , " + "5、onStart()");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, getClass().getSimpleName() + " , " + "6、onResume()");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, getClass().getSimpleName() + " , " + "7、onPause()");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, getClass().getSimpleName() + " , " + "8、onStop()");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, getClass().getSimpleName() + " , " + "9、onDestroyView()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, getClass().getSimpleName() + " , " + "10、onDestroy()");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, getClass().getSimpleName() + " , " + "11、onDetach()");
    }

}